package nowcoder;

/**
 * 链表中倒数最后k个结点
 * @author ：徐梦轲
 * @date ：2022/5/22 0022 18:58
 */
class ListNode {
    int val;
    ListNode next = null;

    public ListNode(int val) {
        this.val = val;
    }
}
public class Solution {
    public ListNode FindKthToTail (ListNode pHead, int k) {
        if (pHead == null|| k <= 0) {
            return null;
        }
        ListNode slow = pHead;
        ListNode fast = pHead;
        for (int i = 0; i < k - 1; i++) {
            fast = fast.next;
            if(fast == null) {
                return null;
            }
        }
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
